import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;

/**
 * 每一行代码，都是改变世界的力量
 *
 * @author 郭帅
 * @date 2022/3/4 11:50 上午
 */
public class Main {

    public static void main(String[] args) {
        GenericKeyedObjectPoolConfig<Connection> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(10);
        config.setMaxWaitMillis(1000);
        GenericKeyedObjectPool<String, Connection> genericKeyedObjectPool =
                new GenericKeyedObjectPool<>(new PoolFactory(), config);

        while (true) {
            try {
                Connection connection = genericKeyedObjectPool.borrowObject("x");
                new Thread(() -> {
                    try {
                        connection.sleep(15000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    genericKeyedObjectPool.returnObject("x", connection);
                }).start();
            } catch (Exception e) {
                System.out.println("拿不到！");
            }
        }
    }
}
